<?php

abstract class McCube_Html_Form_Abstract extends McCube_Html_Abstact{

    public    $name        = '';
    protected $_config     = array();
    protected $_fields     = array();
    protected $_fieldsName = array();
    protected $_errors     = array();
    protected $_model     = null;

    public function __construct($name, array $config, $bundle){
        $this->name        = $name;
        $this->_config     = $config;
        $this ->_bundle    = $bundle;

        if(isset($this->_config['fields']))
            $this->_fieldsName = array_keys($this->_config['fields']);
    }

    public function setModel($model){
        $this->_model = $model;
        return $this;
    }

    public function submit(){
        $button = new McCube_Html_Form_Element_Input('submit', '', $this->_config['submit']);
        return $button->setValue($this->_config['submit']['label']);
    }

    public function setValues($values){
        foreach($this->_fieldsName as $fieldname){
            if(isset($values[$fieldname]))
                $this->field($fieldname)->setValue($values[$fieldname]);
        }
        return $this;
    }

    public function setValue($fieldname, $value){
        $this->field($fieldname)->setValue($value);
        return $this;
    }

    public function getValues(){
        foreach($this->_fieldsName as $fieldname){
            $values[$fieldname] = $this->field($fieldname)->getValue();
        }
        return $values;
    }

    public function getValue($fieldname){
        return $this->field($fieldname)->getValue();
    }

    public function isValid(){
        foreach($this->_fieldsName as $fieldname){
            if(isset($this->_values[$fieldname]))
                $this->field($fieldname)->setValue($this->_values[$fieldname]);

            if(!$this->field($fieldname)->isValid()){
                $this->_errors[] = $fieldname;
            }
        }
        return ($this->_errors ? false : true);
    }

    public function field($fieldname, $silent=false){
        if(!isset($this->_fields[$fieldname])){
            if(!isset($this->_config['fields'][$fieldname])){
                if($silent) return;
                throw new McCube_Html_Form_Exception('Form:Field', 404, array(
                      'fieldname'=> $fieldname
                    , 'formname' => $this->name
                    , 'bundle'   => $this ->_bundle->absoluteName
                ));
            }

            $this->createField(
                  $this->_config['fields'][$fieldname]['type']
                , $fieldname
                , $this->_config['fields'][$fieldname]
            );
        }

        return $this->_fields[$fieldname];
    }

    public function createField($type, $name, $config=array()){
        $_name = $this->name.'['.$name.']';
        switch($type){
            case 'text'     :
            case 'password' :
            case 'number'   :
            case 'file'     :
                $this->_fields[$name] = new McCube_Html_Form_Element_Input($type, $_name, $config);
            break;
            case 'select'  :
                $this->_fields[$name] = new McCube_Html_Form_Element_Select($_name, $config);
            break;
            case 'textarea':
                $this->_fields[$name] = new McCube_Html_Form_Element_Textarea($_name, $config);
            break;
            case 'wysiwyg':
                $this->_fields[$name] = new McCube_Html_Form_Element_Wysiwyg($_name, $config);
            break;
            case 'date':
                $this->_fields[$name] = new McCube_Html_Form_Element_Date($_name, $config);
            break;
            case 'multi-text':
                $this->_fields[$name] = new McCube_Html_Form_Element_MultiText($_name, $config);
            break;
            case 'multi-fields':
                $this->_fields[$name] = new McCube_Html_Form_Element_MultiFields($_name, $config);
            break;
        }
        $this->_fields[$name]->setModel($this->_model);

        return $this->_fields[$name];
    }


}